package com.kylin.core.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kylin.core.R;

/**
 * 下载进度弹窗
 * Created by ywm on 2021/10/18
 */
public class ApkDownloadDialog {


    private static AlertDialog downloadDialog;//正在下载的对话框
    private  static ProgressBar pb;//下载的进度条
    private static TextView tvCur;//当前下载的百分比

    // 展示进度条
    public static void showDownloadDialog(Context context) {
        AlertDialog.Builder adb = new AlertDialog.Builder(context);
        adb.setCancelable(false);// 不可消失
        downloadDialog = adb.create();
        View view = View.inflate(context, R.layout.download_pdf_dialog_layout, null);
        downloadDialog.setView(view, 0, 0, 0, 0);
        tvCur = (TextView) view.findViewById(R.id.tv_cursize_pdf);
        pb = (ProgressBar) view.findViewById(R.id.download_pb_pdf);

        downloadDialog.show();
    }

    // 进度条进度
    public static void showDownloading(Message msg) {

        if(msg!=null && pb!=null){
            // 自己写的
            long totalSize = (long) msg.obj;// 总进度
            int curSize = (int) msg.arg1;// 获取当前进度
            pb.setMax((int) totalSize);
            pb.setProgress(curSize);
        }


    }

    // 关闭进度条
    public static void stopDownloadDialog() {
        // Toast.makeText(getActivity(), "下载成功", Toast.LENGTH_SHORT).show();
        if (downloadDialog != null && downloadDialog.isShowing()) {
            downloadDialog.dismiss();
            // supportFragmentManager.popBackStack();
        }
    }
}
